package com.netease.web.system;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.netease.domain.Commodity;
import com.netease.domain.CommodityModel;
import com.netease.domain.PageInfo;
import com.netease.domain.ShoppingCart;
import com.netease.domain.User;
import com.netease.domain.User_Commodity;
import com.netease.service.CommodityService;
import com.netease.service.ShoppingcartService;
import com.netease.service.UserCommodityService;

@Controller
public class BuyerIndexController {

	@Autowired
	private CommodityService commodityService;

	@Autowired
	private UserCommodityService usercommodityService;

	@Autowired
	private ShoppingcartService shoppingcartService;

	@RequestMapping("buyer/index/all")
	public String index_all(Model model, HttpServletRequest request) {
		String page = request.getParameter("page");
		page = StringUtils.defaultIfBlank(page, "1");
		int pageNumber = Integer.valueOf(page);

		User user = (User) request.getSession().getAttribute("sys_user_key");

		int total = commodityService.findCount();
		PageInfo<CommodityModel> pageInfo = new PageInfo<>(total, pageNumber);

		List<CommodityModel> result = new ArrayList<>();

		List<Commodity> commodity_all = commodityService.findAllByPage(pageInfo.getPageStart());
		List<User_Commodity> commodity_buyed_history = usercommodityService.findAll();

		for (Commodity c : commodity_all) {
			CommodityModel cm = new CommodityModel(c);
			for (User_Commodity uc : commodity_buyed_history) {
				if (c.getId().equals(uc.getCommodityId()) && user.getId().equals(uc.getUser().getId())) {
					cm.setStatus(1);
					break;
				}
			}
			result.add(cm);
		}

		pageInfo.setList(result);

		model.addAttribute("page", pageInfo);
		request.getSession().setAttribute("tab_type", "all");
		return "buyer_index";
	}

	@RequestMapping("buyer/index/no")
	public String index_no(Model model, HttpServletRequest request) {
		String page = request.getParameter("page");
		page = StringUtils.defaultIfBlank(page, "1");
		int pageNumber = Integer.valueOf(page);
		User user = (User) request.getSession().getAttribute("sys_user_key");
		List<User_Commodity> commodity_buyed_history = usercommodityService.findAllByUserId(user.getId());
		List<Commodity> commodity_all = commodityService.findAll();
		List<CommodityModel> commodity_nobuyall = new ArrayList<>();
		int total = commodity_all.size();
		int tempCount = 0;
		for (Commodity c : commodity_all) {
			boolean flag = false;
			for (User_Commodity uc : commodity_buyed_history) {
				if (c.getId().equals(uc.getCommodityId())) {
					flag = true;
					total = total - 1;
					break;
				}
			}

			if (flag == false && commodity_nobuyall.size() <= 8) {
				tempCount++;
				if (tempCount > (pageNumber - 1) * 8 && tempCount <= pageNumber * 8) {
					CommodityModel cm = new CommodityModel(c);
					cm.setStatus(0);
					commodity_nobuyall.add(cm);
				}
			}

		}

		PageInfo<CommodityModel> pageInfo = new PageInfo<>(total, pageNumber);
		pageInfo.setList(commodity_nobuyall);

		model.addAttribute("page", pageInfo);
		request.getSession().setAttribute("tab_type", "no");
		return "buyer_index";
	}

	@RequestMapping("/buyer/account")
	public String account(Model model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("sys_user_key");
		List<User_Commodity> commodity_buyed_history = usercommodityService.findAllByUserId(user.getId());
		model.addAttribute("history", commodity_buyed_history);
		return "account";
	}

	@RequestMapping("/buyer/shoppingcart")
	public String shoppingcart(Model model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("sys_user_key");
		List<ShoppingCart> shoppingcart_commodity = shoppingcartService.findAllByUserId(user.getId());
		model.addAttribute("shoppingcart_commodity", shoppingcart_commodity);
		return "shoppingcart";
	}

	@RequestMapping("/buyer/buy")
	public String buy(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("sys_user_key");
		List<ShoppingCart> shoppingcart_commodity = shoppingcartService.findAllByUserId(user.getId());
		for (ShoppingCart sc : shoppingcart_commodity) {
			User_Commodity uc = new User_Commodity();
			uc.setAmount(sc.getAmount());
			uc.setCommodityId(sc.getCommodityId());
			uc.setContent(sc.getContent());
			uc.setCreateTime(sc.getCreateTime());
			uc.setPicture(sc.getPicture());
			uc.setPrice(sc.getPrice());
			uc.setSummary(sc.getSummary());
			uc.setTitle(sc.getSummary());
			uc.setUser(sc.getUser());
			usercommodityService.save(uc);
			shoppingcartService.delete(sc.getId());
		}

		return "/buyer/account";
	}

	@RequestMapping("buyer/detail")
	public String detail(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		id = StringUtils.defaultIfBlank(id, "1");

		User user = (User) request.getSession().getAttribute("sys_user_key");

		Commodity c = commodityService.findOne(id);
		List<User_Commodity> ucs = usercommodityService.findAllByUserId(user.getId());
		CommodityModel cm = new CommodityModel(c);
		for (User_Commodity uc : ucs) {
			if (uc.getCommodityId().equals(id)) {
				cm.setStatus(1);
				cm.setPreprice(uc.getPrice());
				break;
			}
		}
		
		model.addAttribute("commodity", cm);
		return "buyer_detail";
	}
}
